package com.i2works.smartluolong.api.controller.merchant;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.i2works.smartluolong.api.controller.common.CommonItemController;
import com.i2works.smartluolong.utils.base.*;
import com.i2works.smartluolong.utils.common.BaseUtils;
import com.i2works.smartluolong.utils.common.DateUtils;
import com.i2works.smartluolong.utils.entity.R;
import com.i2works.smartluolong.utils.entity.Result;
import com.i2works.smartluolong.utils.exception.ServiceException;
import com.i2works.smartluolong.web.entity.Implement.ImplementDO;
import com.i2works.smartluolong.web.entity.Implement.ImplementQuery;
import com.i2works.smartluolong.web.entity.flow.FlowInfoDO;
import com.i2works.smartluolong.web.entity.item.ItemApasinfoDO;
import com.i2works.smartluolong.web.entity.item.ItemApasinfoQuery;
import com.i2works.smartluolong.web.entity.item.ItemAttrDO;
import com.i2works.smartluolong.web.entity.item.ItemNodeDO;
import com.i2works.smartluolong.web.entity.item.ItemNodeQuery;
import com.i2works.smartluolong.web.entity.merchant.*;
import com.i2works.smartluolong.web.service.flow.IFlowProcessService;
import com.i2works.smartluolong.web.service.flow.IFlowService;
import com.i2works.smartluolong.web.service.flow.IFlowTemplateService;
import com.i2works.smartluolong.web.service.implement.IImplementService;
import com.i2works.smartluolong.web.service.item.IItemApasinfoService;
import com.i2works.smartluolong.web.service.item.IItemAttrService;
import com.i2works.smartluolong.web.service.item.IItemNodeService;
import com.i2works.smartluolong.web.service.merchant.IMerchantApplyImagesService;
import com.i2works.smartluolong.web.service.merchant.IMerchantApplyMemberService;
import com.i2works.smartluolong.web.service.merchant.IMerchantApplyService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * 个体工商户申请 - 控制器
 *
 * @author Liu Yutao <koal@vip.qq.com>
 * @date 2018/3/15 9:48
 */
@RestController
@RequestMapping("/merchantApply")
public class MerchantApplyController extends CommonItemController {

	 private final IMerchantApplyService merchantApplyService;
	    private final IMerchantApplyImagesService merchantApplyImagesService;
	    private final IMerchantApplyMemberService merchantApplyMemberService;
	    private final IItemApasinfoService  itemApasinfoService;
	    private final IItemAttrService itemAttrService;
	    private final IItemNodeService  itemNodeService;
	    private final IImplementService implementService;

	    public MerchantApplyController(IFlowTemplateService flowTemplateService,
	    		IFlowService flowService, 
	    		IFlowProcessService flowProcessService, 
	    		IMerchantApplyService merchantApplyService, 
	    		IMerchantApplyImagesService merchantApplyImagesService, 
	    		IMerchantApplyMemberService merchantApplyMemberService,
	    		IItemApasinfoService  itemApasinfoService,
	    		IItemAttrService itemAttrService,
	    		IItemNodeService  itemNodeService,
	    		IImplementService implementService
	    		) {
	        super(flowTemplateService, flowService, flowProcessService);
	        this.merchantApplyService = merchantApplyService;
	        this.merchantApplyImagesService = merchantApplyImagesService;
	        this.merchantApplyMemberService = merchantApplyMemberService;
	        this.itemApasinfoService=itemApasinfoService;
	        this.itemAttrService=itemAttrService;
	        this.itemNodeService=itemNodeService;
	        this.implementService=implementService;
	    }


    /**
     * 查看工商申请 - 页面
     */
    @GetMapping(value = "/view/{id:\\d+}")
    public Result query(@PathVariable Integer id) {
        HashMap<String, Object> hashMap = Maps.newLinkedHashMap();
        try {
            //查询详情
            MerchantApplyQuery applyQuery = new MerchantApplyQuery();
            applyQuery.setId(id);
            MerchantApplyDO merchantApply = merchantApplyService.query(applyQuery);
            if (null != merchantApply) {
                hashMap.put("merchantApply", merchantApply);
                String[] merchantBrandOptionalList = StringUtils.split(merchantApply.getMerchantBrandOptional(), ",");
                hashMap.put("merchantBrandOptionalList", merchantBrandOptionalList);
                //查询经营者个人信息
                MerchantApplyMemberQuery memberQuery = new MerchantApplyMemberQuery();
                memberQuery.setApplyId(merchantApply.getId());
                memberQuery.setStatus(GlobalConstant.status.TRUE);
                MerchantApplyMemberDO merchantApplyMemberDO = merchantApplyMemberService.query(memberQuery);
                hashMap.put("member", merchantApplyMemberDO);
                //查询资料图片
                MerchantApplyImagesQuery imagesQuery = new MerchantApplyImagesQuery();
                imagesQuery.setApplyId(merchantApply.getId());
                List<MerchantApplyImagesDO> imagesList = merchantApplyImagesService.queryList(imagesQuery);
                hashMap.put("imagesList", imagesList);
                //查询流程
                hashMap.put("flowProcessList", queryFlowProcess(id, FlowItemTypeEnum.MERCHANT_APPLY.getKey()));
            } else {
                return R.error(MsgEnum.FIND_ERROR.getMsg());
            }
        } catch (Exception e) {
            logger.error("", e);
            return R.error(MsgEnum.FIND_ERROR.getMsg());
        }
        return R.success(hashMap);
    }


    /**
     * 个体工商户基本信息(回显)
     */
    @GetMapping("/base/{applyId:\\d+}")
    public Result base(@PathVariable(value = "applyId") Integer applyId) {
        HashMap<String, Object> hashMap = Maps.newHashMap();
        try {
            //查询申请ID是不是当前用户的
            MerchantApplyQuery merchantApplyQuery = new MerchantApplyQuery();
            merchantApplyQuery.setUserId(85);
//            merchantApplyQuery.setUserId(getUserId());
            merchantApplyQuery.setId(applyId);
            MerchantApplyDO merchantApply = merchantApplyService.query(merchantApplyQuery);
            if (null == merchantApply) {
                return R.error(MsgEnum.FIND_ERROR.getMsg());
            }
            //状态判断
            if (existUpdate(merchantApply.getApplyStatus())) {
                return R.error(MsgEnum.ITEM_SUCCESS.getMsg());
            }
            //基本资料
            hashMap.put("merchantApply", merchantApply);
            //备选字号
            String[] brandOptionalList = merchantApply.getMerchantBrandOptional().split(",");
            hashMap.put("brandOptionalList", brandOptionalList);
            //申请图片
            MerchantApplyImagesQuery imagesQuery = new MerchantApplyImagesQuery();
            imagesQuery.setApplyId(merchantApply.getId());
            Integer queryCount = merchantApplyImagesService.queryCount(imagesQuery);
            List<MerchantApplyImagesDO> applyImagesList = null;
            if (queryCount > 0) {
                applyImagesList = merchantApplyImagesService.queryList(imagesQuery);
            }
            hashMap.put("merchantApplyImagesList", applyImagesList);
        } catch (ServiceException e) {
            logger.error("个体工商户基本信息(回显)异常：" + e.getMessage());
        }
        return R.success(hashMap);
    }


    /**
     * 新增基本信息
     */
    @PostMapping("/save")
    public Result save(@ModelAttribute MerchantApplyDO merchantApply) {
        try {
            //用户信息
            merchantApply.setUserId(getUserId());
            //创建人信息
            merchantApply.setUpdateName(getUserName());
            //个体工商户名称补充完整
            merchantApply.setMerchantName(MerchantConstant.base.REGION_PREFIX + merchantApply.getMerchantBrand() + merchantApply.getIndustry() + merchantApply.getMerchantType());
            //地址补充完整
            merchantApply.setRegion(MerchantConstant.base.REGION_PREFIX);
            merchantApply.setMerchantAddress(MerchantConstant.base.ADDRESS_PREFIX + merchantApply.getMerchantAddress());
            //备用字号格式化
            String merchantBrandOptional = BaseUtils.listToStr(merchantApply.getMerchantBrandOptionalList(), ",");
            merchantApply.setMerchantBrandOptional(merchantBrandOptional);
            //状态
            merchantApply.setApplyStatus(MerchantStatusEnum.STATUS_0.getKey());
            //保存
            merchantApply.setUpdateName(getUserName());
            merchantApply.setCreateName(getUserName());
            merchantApplyService.saveOrUpdate(merchantApply);
            return R.success(merchantApply);
        } catch (ServiceException e) {
            logger.error("添加商户个人信息异常：" + e.getMessage());
            return R.error(MsgEnum.SAVE_ERROR.getMsg());
        }
    }


    /**
     * 编辑基本信息（单个）
     */
    @PostMapping("/singleUpdate")
    public Result singleUpdate(@RequestParam("id") Integer applyId, @RequestParam("name") String name, @RequestParam("value") String value) {

        MerchantApplyDO merchantApply = new MerchantApplyDO();
        merchantApply.setUpdateName(getUserName());
        merchantApply.setCreateName(getUserName());
        try {
            merchantApplyService.update(merchantApply);
        } catch (ServiceException e) {
            logger.error("添加商户个人信息异常：" + e.getMessage());
            return R.error(MsgEnum.ATTRS_EMPTY.getMsg());
        }
        return R.success();
    }


    /**
     * 添加商户个人信息
     */
    @GetMapping("/member/{applyId:\\d+}")
    public Result shareholderPage(@PathVariable(value = "applyId") Integer applyId) {
        MerchantApplyDO merchantApply = getMerchantApply(applyId);
        if (null == merchantApply) {
            return R.error(MsgEnum.CHECK_FALSE.getMsg());
        }
        //状态判断
        if (existUpdate(merchantApply.getApplyStatus())) {
            return R.error(MsgEnum.ITEM_SUCCESS.getMsg());
        }
        //hashMap.put("merchantApply", merchantApply);
        //查询商户个人信息信息(回显)
        MerchantApplyMemberQuery memberQuery = new MerchantApplyMemberQuery();
        memberQuery.setApplyId(merchantApply.getId());
        MerchantApplyMemberDO memberDO;
        try {
            memberDO = merchantApplyMemberService.query(memberQuery);
        } catch (ServiceException e) {
            logger.error("添加商户个人信息人信息异常：" + e.getMessage());
            return R.error(MsgEnum.SYSTEM_ERROR.getMsg());
        }
        return R.success(memberDO);
    }


    /**
     * 添加商户个人信息
     */
    @PostMapping("/member/save")
    public Result saveMemeber(@ModelAttribute MerchantApplyMemberDO merchantApplyMember) {
        merchantApplyMember.setUpdateName(getUserName());
        merchantApplyMember.setCreateName(getUserName());
        merchantApplyMember.setStatus(GlobalConstant.status.TRUE);
        try {
            MerchantApplyQuery merchantApplyQuery = new MerchantApplyQuery();
            merchantApplyQuery.setId(merchantApplyMember.getApplyId());
            MerchantApplyDO merchantApplyDO = merchantApplyService.query(merchantApplyQuery);
            if (merchantApplyDO == null) {
                return R.error(MsgEnum.SAVE_ERROR.getMsg());
            }
            merchantApplyMemberService.saveMember(merchantApplyMember, merchantApplyDO);
        } catch (ServiceException e) {
            logger.error("添加商户个人信息异常：" + e.getMessage());
            return R.error(MsgEnum.ATTRS_EMPTY.getMsg());
        }
        return R.success();
    }


    /**
     * 申请成功提示
     */
    @GetMapping("/success/{applyId:\\d+}")
    public Result success(@PathVariable("applyId") Integer applyId) {
        HashMap<String, Object> hashMap = Maps.newHashMap();
        //查询申请ID是不是当前用户的
        MerchantApplyQuery merchantApplyQuery = new MerchantApplyQuery();
        merchantApplyQuery.setUserId(getUserId());
        merchantApplyQuery.setId(applyId);
        try {
            MerchantApplyDO merchantApply = merchantApplyService.query(merchantApplyQuery);
            if (null == merchantApply) {
                return R.error(MsgEnum.CHECK_FALSE.getMsg());
            } else {
                //更新状态
                //merchantApply.setApplyStatus(MerchantStatusEnum.STATUS_0.getKey());
                //fixme 如果添加自拍和签名，需要设置status = 0 如上段代码
                merchantApply.setApplyStatus(MerchantStatusEnum.STATUS_1.getKey());
                merchantApplyService.update(merchantApply);
                //写入记录表
            }
            SaveItemApasinfo(applyId);//插入省统一申报表
            hashMap.put("merchant", merchantApply);
            hashMap.put("merchantStatus", getApplyStatus(merchantApply.getApplyStatus()));
        } catch (ServiceException e) {
            logger.error("申请成功异常：" + e.getMessage());
            return R.error(MsgEnum.SYSTEM_ERROR.getMsg());
        }
        return R.success(hashMap);
    }


    /**
     * 获取需要签名的人员信息
     */
    @GetMapping("/autograph/{applyId:\\d+}")
    public Result autograph(@PathVariable Integer applyId) {
        MerchantApplyMemberQuery memberQuery = new MerchantApplyMemberQuery();
        memberQuery.setApplyId(applyId);
        try {
            MerchantApplyMemberDO memberDO = merchantApplyMemberService.query(memberQuery);
            if (memberDO != null) {
                return R.success(Lists.newArrayList(memberDO), 1);
            }
        } catch (ServiceException e) {
            logger.error("查询数据补全用户列表失败：" + e.getMessage(), applyId);
            return R.error(MsgEnum.FIND_ERROR.getMsg());
        }
        return R.error(MsgEnum.FIND_ERROR.getMsg());
    }


    /**
     * 获取需要签名的人员信息
     */
    @PostMapping("/autograph/save")
    public Result autographSave(@ModelAttribute MerchantApplyMemberDO memberDO) {
        try {
            MerchantApplyMemberQuery memberQuery = new MerchantApplyMemberQuery();
            memberQuery.setId(memberDO.getId());
            MerchantApplyMemberDO resultMember = merchantApplyMemberService.query(memberQuery);
            if (resultMember != null) {
                merchantApplyMemberService.update(memberDO);
                //更新申请状态
                MerchantApplyDO merchantApplyDO = new MerchantApplyDO();
                merchantApplyDO.setId(resultMember.getApplyId());
                merchantApplyDO.setApplyStatus(MerchantStatusEnum.STATUS_1.getKey());
                merchantApplyService.update(merchantApplyDO);
                //添加工作流
                FlowInfoDO flowInfoDO = new FlowInfoDO();
                flowInfoDO.setItemId(merchantApplyDO.getId());
                flowInfoDO.setComplete(0);
                flowInfoDO.setItemName(merchantApplyDO.getMerchantName() + "-" + FlowItemTypeEnum.MERCHANT_APPLY.getValue());
                flowInfoDO.setItemType(FlowItemTypeEnum.MERCHANT_APPLY.getKey());
                flowInfoDO.setItemStatus(MerchantStatusEnum.STATUS_1.getKey());
                flowInfoDO.setUserId(resultMember.getId());
                flowInfoDO.setUpdateName(resultMember.getName());
                flowInfoDO.setMessage("用户《" + flowInfoDO.getUpdateName() + "》提交审核");
            }
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return R.error(MsgEnum.FIND_ERROR.getMsg());
    }


    /**
     * 获取申请状态
     */
    private String getApplyStatus(Integer key) {
        MerchantStatusEnum[] enums = MerchantStatusEnum.values();
        for (MerchantStatusEnum applyStatusEnum : enums) {
            if (applyStatusEnum.getKey().equals(key)) {
                return applyStatusEnum.getText();
            }
        }
        return MerchantStatusEnum.STATUS_0.getText();
    }


    /**
     * 查询申请实体(用户自己的)
     */
    private MerchantApplyDO getMerchantApply(Integer applyId) {
        MerchantApplyQuery applyQuery = new MerchantApplyQuery();
        applyQuery.setUserId(getUserId());
        applyQuery.setId(applyId);
        try {
            return merchantApplyService.query(applyQuery);
        } catch (ServiceException e) {
            logger.error("申请编号{}查询失败：" + e.getMessage(), applyId);
        }
        return null;
    }

    /**
     * 是否可更新
     *
     * @param itemStatus 业务状态
     * @return 如果包含，就返回true，不可再编辑了。
     * STATUS_1(1, "待审核"),
     * STATUS_2(2, "审核中"),
     * STATUS_4(4, "审核通过"),
     * STATUS_5(5, "作废");
     */
    private boolean existUpdate(Integer itemStatus) {
        List<Integer> list = Lists.newArrayList(MerchantStatusEnum.STATUS_1.getKey(), MerchantStatusEnum.STATUS_2.getKey(), MerchantStatusEnum.STATUS_4.getKey(), MerchantStatusEnum.STATUS_5.getKey());
        return list.contains(itemStatus);
    }
    
    
    
    private void SaveItemApasinfo(Integer id) {
      	   try{
    		    
    		     ImplementQuery query=new ImplementQuery();
    		     query.setServiceCode("K24514909XK09751002");
    		     query.setStatus("Y");
    		     ImplementDO  implement=implementService.query(query);
    		     ItemApasinfoDO apasinfo=new ItemApasinfoDO();
    	         String projectId=null;
    	    	 projectId=createProjectId(implement.getDeptCode());
    	    	 /**
    	    	  *个体户申请表加projId
    	    	  * */
    	    	 MerchantApplyQuery comQuery=new MerchantApplyQuery();
    		     comQuery.setId(id);
    		     MerchantApplyDO merchantApply =merchantApplyService.query(comQuery);
    		     merchantApply.setId(id);
    		     merchantApply.setProjectId(projectId);
    		     merchantApplyService.update(merchantApply);
    	    	 apasinfo.setProjId(projectId);
    	    	 apasinfo.setProjPwd(createProjectPWD());
    	    	 apasinfo.setServiceCode(implement.getServiceCode());
    	    	 apasinfo.setServiceName(implement.getServiceName());
    	    	 apasinfo.setInfoType(implement.getInfoType());
    	    	 MerchantApplyMemberQuery memberQuery=new MerchantApplyMemberQuery();
    	    	 memberQuery.setApplyId(merchantApply.getId());
    	    	 memberQuery.setStatus(1);
    	    	 MerchantApplyMemberDO legal= merchantApplyMemberService.query(memberQuery);
    	    	 apasinfo.setApplyName(legal.getName());
    	    	 apasinfo.setApplyCardType("身份证");
    	    	 apasinfo.setApplyCardNumber(legal.getCardNumber());
    	    	 apasinfo.setContactMan(legal.getName());
    	    	 apasinfo.setContactManCardType("身份证");
    	    	 apasinfo.setContactManCardNumber(legal.getCardNumber());
    	    	 apasinfo.setTelphone(legal.getPhone());
    	    	 apasinfo.setPhone(legal.getPhone());
    	    	 apasinfo.setPostCode("");
    	    	 apasinfo.setAddress(merchantApply.getMerchantAddress());
    	    	 apasinfo.setDeptCode(implement.getDeptCode());
    	    	 apasinfo.setDeptName(implement.getDeptName());
    	    	 apasinfo.setApplyFrom("政务服务网");
    	    	 apasinfo.setApplyType(implement.getApplyType());
    	    	 apasinfo.setApplyPropertiy(implement.getApplyPropertiy());
    	    	 apasinfo.setReceiveTime(merchantApply.getCreateTime());
    	    	 apasinfo.setPromiseDays(implement.getPromiseDays());
    	    	 apasinfo.setLegalDays(implement.getLegalDays());
    	    	 apasinfo.setHandleState("收件");//办理状态
    	    	 apasinfo.setAreaCode(implement.getAreaCode());
    	    	 String belongSystem = apasinfo.getAreaCode()+apasinfo.getDeptCode()+"001";
    	    	 apasinfo.setBelongSystem(belongSystem);
    	    	 apasinfo.setReceiveUserid(getUserOpenId());
    	    	 apasinfo.setReceiveName(getUserName());
    	    	 apasinfo.setQlKind(getQLKind(implement.getServiceType()));
    	    	 apasinfo.setCreateName(merchantApply.getCreateName());
    	    	 apasinfo.setUpdateName(merchantApply.getUpdateName());
    	    	 apasinfo.setSyncStatus("I");
    	    	 saveAttr(merchantApply);
    			 ItemNodeDO  itemNode=new ItemNodeDO();
    			 itemNode.setProjId(apasinfo.getProjId());
    	         itemNode.setAction("收件");
    	         itemNode.setOpinion("电子件已接收");
    	         itemNode.setNodeName("收件");
    	         itemNode.setName(apasinfo.getApplyName());
    	         itemNode.setStartTime(new Date());
    	         itemNode.setEndTime(new Date());
    	         itemNode.setBelongSystem(apasinfo.getBelongSystem());
    	         itemNode.setCreateTime(new Date());
    	         itemNode.setDataVersion(1);
    	         itemNode.setNextNodeName("受理");
    	         itemNode.setAreaCode(apasinfo.getAreaCode());
    	         itemNode.setHanderDeptId(apasinfo.getDeptCode());
    	         itemNode.setHanderDeptName(apasinfo.getDeptName());
    	         itemNode.setPhaseName("申报阶段");
    	         itemNode.setPhaseCode("01");
    	         itemNode.setSyncStatus("I");
    			 itemNodeService.save(itemNode);
    	    	 apasinfo.setDataVersion(1);
    	    	 apasinfo.setFlowXml(getFlowXML(apasinfo));//流程xml
    			 itemApasinfoService.save(apasinfo);
    	    } catch (ServiceException e) {
    	      logger.error("apasinfo收件环节插入异常"+e.getMessage());
    	    }
    }
    
    /**
     * 处理附件
     *
     * @param MerchantApplyId 公司申请id
     */
    private void saveAttr(MerchantApplyDO MerchantApply) throws ServiceException {
    	MerchantApplyImagesQuery imagesQuery=new MerchantApplyImagesQuery();
    	imagesQuery.setApplyId(MerchantApply.getId());
    	imagesQuery.setStatus(1);
        List<MerchantApplyImagesDO> list=merchantApplyImagesService.queryList(imagesQuery);
        for (MerchantApplyImagesDO imagesDO : list) {
        	  ItemAttrDO attrDO = new ItemAttrDO();
              attrDO.setCreateName(getUserName());
              attrDO.setProjId(MerchantApply.getProjectId());
              attrDO.setAttrName("个体工商户注册登记申请材料");
              attrDO.setFileUrl(imagesDO.getImg());
              attrDO.setFileName("个体工商户注册登记申请材料");
              itemAttrService.save(attrDO);
		}
    }
    

    /**
     * 
     * 生成办件编号
     * */
    private String createProjectId(String deptCode) {
  	  String projId=null;
  	  int num=1;
  	  try {
  		  int length=deptCode.length();
  		  String applyDate = DateUtils.get(new Date(), DateConstant.YYMMDD);
  		  ItemApasinfoQuery query=new ItemApasinfoQuery();
  		  query.setReceiveTime(DateUtils.str2Date(applyDate, DateConstant.YYMMDD));
  		  query.setDeptCode(deptCode);
  		  num = itemApasinfoService.queryCount(query);
  		  num=num+1;
  		  projId = "410311" +deptCode.substring(length-3)+ applyDate + '1' + String.format("%06d", num);
  	  } catch (ServiceException e) {
  		  e.printStackTrace();
  	  }
  	  return projId;
    }
    
    /**
     * 生成查询密码
     * 
     * */
    private String createProjectPWD() {
  	  String sources = "0123456789"; 
  		Random rand = new Random();
  		StringBuffer flag = new StringBuffer();
  		for (int j = 0; j < 6; j++) 
  		{
  			flag.append(sources.charAt(rand.nextInt(9)) + "");
  		}
  	  return flag.toString();
    }
    
    /**
     * 生成流程XML
     */
    private String getFlowXML(ItemApasinfoDO apasinfoDO) {
            String xml = "<?xml version=\"1.0\" encoding=\"GBK\"?>\r\n<NODES>\r\n";
            try {
            List<ItemNodeDO> list=Lists.newArrayList();
            ItemNodeQuery query=new ItemNodeQuery();
            query.setProjId(apasinfoDO.getProjId());
            query.setOrder(0);
            Integer	count = itemNodeService.queryCount(query);
            if(count>0) {
          	  list=itemNodeService.queryList(query);
          	  int step=1;
          	  for (ItemNodeDO itemNodeDO : list) {
                    xml += "<NODE>\r\n" +
                            "    <NODENAME>"+itemNodeDO.getNodeName()+"</NODENAME>\r\n" +
                            "    <STEP>"+step+"</STEP>\r\n" +
                            "    <STATE>"+(4-step)+"</STATE>\r\n" +
                            "    <TIMELIMIT>"+apasinfoDO.getPromiseDays()+"</TIMELIMIT>\r\n"+
                            "    <STARTTIME>"+DateUtils.get(itemNodeDO.getStartTime(), DateConstant.ALL)+"</STARTTIME>\r\n"+
                            "    <ENDTIME>"+DateUtils.get(itemNodeDO.getEndTime(), DateConstant.ALL)+"</ENDTIME>\r\n"+
                            "    <STAYTIME>0</STAYTIME>\r\n"+
                            "    <OPINION>" + itemNodeDO.getOpinion() + "</OPINION>\r\n" +
                            "    <NAME>"+itemNodeDO.getName()+"</NAME>\r\n" +
                            "    <TEL>"+apasinfoDO.getPhone()+"</TEL>\r\n" +
                            "</NODE>\r\n";
                    step++;
  			  }
            }
            xml += "</NODES>";
  		} catch (ServiceException e) {
  			e.printStackTrace();
  		}
        return xml;
    }
    /**
     * 获取权力类型
     * */
    
    private String getQLKind(String  key){
    	Map<String, String> map =new HashMap<String, String> ();
    		map.put("XK", "行政许可");
    		map.put("JC", "行政检查");
    		map.put("QZ", "行政强制");
    		map.put("ZC", "行政征收");
    		map.put("GF", "行政给付");
    		map.put("CJ", "行政裁决");
    		map.put("QR", "行政确认");
    		map.put("JL", "行政奖励");
    		map.put("GG", "公共服务");
    		map.put("QT", "其他权力");
    		String QLKing=map.get(key);
    	return QLKing;
    }

    


}
